﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Framework.Rule;
using PlanningGIS.Util;
using PlanningGIS.Engine.Controls;
using PlanningGIS.Win.Controls;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Framework.Controls
{
    /// <summary>
    /// 数据入库规则控件
    /// </summary>
    public partial class CtlImpRule : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlImpRule()
        {
            InitializeComponent();
            List<ValueName> ds = new List<ValueName>();
            ds.Add(new ValueName() { Name = "K 1:500", Value = "K" });
            ds.Add(new ValueName() { Name = "I 1:2000", Value = "I" });
            ds.Add(new ValueName() { Name = "G 1:10000", Value = "G" });
            ds.Add(new ValueName() { Name = "F 1:25000", Value = "F" });
            cmbAffix.ValueMember = "Value";
            cmbAffix.DisplayMember = "Name";
            cmbAffix.DataSource = ds;
        }

        /// <summary>
        /// 加载控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CtlImpRule_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 是否显示默认选择框
        /// </summary>
        public bool DefaultVisible
        {
            get { return chkIsDefault.Visible; }
            set { chkIsDefault.Visible = value; }
        }

        /// <summary>
        /// 规则信息
        /// </summary>
        private DataImportRule _Rule = null;

        /// <summary>
        /// 获取或设置规则信息
        /// </summary>
        public DataImportRule Rule
        {
            get
            {
                GetRule();
                return _Rule;
            }
            set
            {
                _Rule = value; 
                SetRule();
            }
        }

        /// <summary>
        /// 给规则赋值
        /// </summary>
        private void SetRule()
        {
            if (_Rule == null) return;
            txtRuleName.Text = _Rule.RuleName;
            chkIsDefault.Checked = _Rule.IsDefault;
            txtDataSetName.Text = _Rule.DatasetName;
            txtAffix.Text = _Rule.Affix;
            if ("localhost".Equals(_Rule.Server, StringComparison.CurrentCultureIgnoreCase))
                txtIP.Value = "127.0.0.1";
            else txtIP.Value = _Rule.Server;
            txtOrcl.Text = _Rule.Database;
            txtUser.Text = _Rule.User;
            txtPassword.Text = _Rule.Password;
            txtPath.Text = _Rule.Path;
            txtField.Text = _Rule.SourceFieldName;
            if (_Rule.WorkspaceType.Equals("sde", StringComparison.CurrentCultureIgnoreCase))
                supertbctlDb.SelectedTab = supertbitmSde;
            else
                supertbctlDb.SelectedTab = supertbitmLocal;
        }

        /// <summary>
        /// 更新规则的的信息
        /// </summary>
        private void GetRule()
        {
            if (_Rule == null) return;
            if (supertbctlDb.SelectedTab == supertbitmSde)
            {
                _Rule.WorkspaceType = "sde";                
            }
            else
            {
                _Rule.WorkspaceType = "local";
            }
            _Rule.RuleName = txtRuleName.Text;
            _Rule.IsDefault = chkIsDefault.Checked;
            _Rule.DatasetName = txtDataSetName.Text;
            _Rule.Affix = txtAffix.Text;
            _Rule.Server = txtIP.Value;
            _Rule.Database = txtOrcl.Text;
            _Rule.User = txtUser.Text;
            _Rule.Password = txtPassword.Text;
            _Rule.Path = txtPath.Text;
            _Rule.SourceFieldName = txtField.Text;            
        }

        /// <summary>
        /// 选择后缀
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbAffix_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtAffix.Text = "_" + cmbAffix.SelectedValue.ToString();
        }

        /// <summary>
        /// 选择已有的工作区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenDataDialog dlg = new OpenDataDialog();
            dlg.MultiSelect = false;
            if (dlg.ShowDialog(this) != DialogResult.OK) return;            
            if (dlg.SelectWorkspace == null) return;
            this.Cursor = Cursors.WaitCursor;
            try
            {
                if (dlg.SelectWorkspace.WorkspaceFactory.WorkspaceType == ESRI.ArcGIS.Geodatabase.esriWorkspaceType.esriRemoteDatabaseWorkspace)
                {
                    Dictionary<string, dynamic> properties = dlg.SelectWorkspace.GetProperties();
                    txtIP.Value = properties["Server".ToUpper()];
                    txtUser.Text = properties["User".ToUpper()]; 
                    //txtPassword.Text = dlg.SelectWorkspace.GetPropertyValue("Password");
                    txtOrcl.Text = properties["Instance".ToUpper()];
                    if (txtOrcl.Text.Contains("/")) txtOrcl.Text = txtOrcl.Text.Split('/')[1];
                    //由于密码加密了，不知道怎么解析
                    //dynamic psd = properties["Password".ToUpper()];
                    //if (psd is byte[]) txtPassword.Text = System.Text.Encoding.Unicode.GetString((byte[])psd);
                    //else txtPassword.Text = psd.ToString();
                    supertbctlDb.SelectedTab = supertbitmSde;
                }
                else
                {
                    txtPath.Text = dlg.SelectWorkspace.GetPropertyValue("Database");
                    supertbctlDb.SelectedTab = supertbitmLocal;
                }
            }
            catch (Exception ex)
            {
                MsgHelper.ShowErrorMsgbox(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRule_Click(object sender, EventArgs e)
        {
            FrmSelImpRule frm = new FrmSelImpRule();
            if (frm.ShowDialog(this) != DialogResult.OK) return;
            Rule = frm.Rule as DataImportRule;
        }
    }
}
